﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;

using LEEAAttendanceControl.Web;
using LEEAAttendanceControl.Web.Services;

using System.ServiceModel.DomainServices.Client;

using System.Text;
using LEEAAttendanceControl.General;

namespace LEEAAttendanceControl.Views
{
    public partial class EmployeesShifts : Page
    {
        LEEAContext context = new LEEAContext();

        public EmployeesShifts()
        {
            InitializeComponent();
        }

        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            App myApp = (App)Application.Current;

            myApp.ClearEvents();

            myApp.EditEvent += EditEmployeeShifts;
            myApp.DeleteEvent += DeleteEmployeeShifts;

            myApp.ChangeEmployeesEvent += ChangeEmployeesHandler;

            ChangeEmployees();
        }

        public void ChangeEmployeesHandler(object sender, EventArgs e)
        {
            ChangeEmployees();
        }

        private void DomainDataSource_LoadingData(object sender, LoadingDataEventArgs e)
        {
            e.LoadBehavior = LoadBehavior.RefreshCurrent;
        }

        void ChangeEmployees()
        {

            ShAssignmentDataSource.QueryParameters.Clear();

            App app=(App)Application.Current;

            Parameter pc = new Parameter();

            pc.ParameterName = "companyid";
            pc.Value = app.CompanyID.HasValue?app.CompanyID.Value:No.Zero;
            ShAssignmentDataSource.QueryParameters.Add(pc);

            Parameter pd = new Parameter();

            pd.ParameterName = "departmentid";
            pd.Value = app.DepartmentID.HasValue?app.DepartmentID.Value:No.Zero;
            ShAssignmentDataSource.QueryParameters.Add(pd);

            Parameter pe = new Parameter();

            pe.ParameterName = "employeeid";
            pe.Value = app.EmployeeId;
            ShAssignmentDataSource.QueryParameters.Add(pe);

            ShAssignmentDataSource.Load();
        }

        public void EditEmployeeShifts(object sender, EventArgs e)
        {
            ShiftAssignmentSummary summary = ShAssignmentsGrid.SelectedItem as ShiftAssignmentSummary;

            if (summary != null)
            {
                ShiftAssignmentEdit shAssignEdit = new ShiftAssignmentEdit();

                shAssignEdit.Closed += ShiftAssignmentEdit_Closed;

                shAssignEdit.LoadData(summary);

                shAssignEdit.Show();
            }
        }

        public void ShiftAssignmentEdit_Closed(object sender, EventArgs e)
        {
            ShAssignmentDataSource.Load();
        }

        public void DeleteEmployeeShifts(object sender, EventArgs e)
        {
            if (ShAssignmentsGrid.SelectedItem != null)
            {
                MessageDisplay message = new MessageDisplay(ApplicationStrings.DeleteMessage
                    , ApplicationStrings.Delete, "Ask");

                message.Closed += (s, ee) =>
                {
                    if (message.DialogResult.Value)
                    {
                        int index = ShAssignmentsGrid.SelectedIndex;

                        ShAssignmentDataSource.DataView.Remove(ShAssignmentsGrid.SelectedItem);

                        ShAssignmentDataSource.SubmittedChanges += (ss, eee) =>
                        {
                            if (eee.HasError)
                            {
                                eee.MarkErrorAsHandled();
                                MessageDisplay message2 = new MessageDisplay(ApplicationStrings.GeneralError
                                    , ApplicationStrings.GeneralError, "Error");

                                message2.Show();
                            }
                        };

                        ShAssignmentDataSource.SubmitChanges();

                        ShAssignmentsGrid.SelectedIndex = index > 0 ? index - 1 : 0;
                    }
                };

                message.Show();
            }
        }
    }
}
